package com.aaa.ps.service;

import com.aaa.common.entity.Role;
import com.aaa.ps.dao.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * fileName:RoleServiceImpl
 * description:
 * author:zz
 * createTime:2020/8/28 16:30
 * version:1.0.0
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int deleteByPrimaryKey(Integer roleId) {
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int insertSelective(Role record) {
        return roleMapper.insertSelective(record);
    }

    @Override
    public Role selectByPrimaryKey(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int batchInsertRoleMenu(int roleId, String menuIds) {
        //roleId   10   menuIds  "103,119,120,130,131"
        int r1 = roleMapper.deleteRoleMenuByRoleId(roleId);
        int r2 =0;
        //判断是否为空
        if(!StringUtils.isEmpty(menuIds)){
            //分割字符串
            String[] midArray = menuIds.split(",");
            StringBuffer stringBuffer  = new StringBuffer();
            //循环拼装sql
            for (String mid : midArray) {
                stringBuffer.append("("+roleId+","+mid+"),");
            }
            //去掉最后一个逗号
            String values = stringBuffer.substring(0,stringBuffer.length()-1);
            // 调用添加方法
            r2=roleMapper.batchInsertRoleMenu(values);
        }
        //判断上面业务都执行成功
        if(r1>0&&r2>0){
            return 1;
        }
        return 0;
    }
}
